<template>
	<view>
		<!-- 下拉加载更多 -->
		<scroll-view class="orderList" scroll-y :style="{ height: mainHeight + 'px' }" :lower-threshold="50"
			@scrolltolower="loadMoreData">
			<!-- <view v-for="(item, index) in list" :key="index" class="orderItem">
				<view>{{ item.memo }}</view>
				<view>{{ item.type }}</view>
			</view> -->
			<view class="orderItem" v-for="(item, index) in list" :key="index">
				<view class="OL">
					<image :src="item.image" mode=""></image>
				</view>
				<view class="OR">
					<view class="ORT">
						{{item.name}}
					</view>
					<view class="ORM">
						<text>{{item.balance}}</text>元+<text>{{item.point}}</text>积分+<text>{{item.jinbi}}</text>抵用券
					</view>
					<!-- <view class="ORB" @click="tihuo(item.order_id)" v-if="item.tihuo==1&&item.status==1"> -->
					<view class="ORB" @click="tihuo2(item.order_id)" v-if="item.status==1 && item.tihuo == 0">
						立即提货
					</view>
					<view class="ORBB" @click="fahuo(item.order_id)" v-else-if="item.status==1 && item.tihuo == 3">
						我要发货
					</view>
					<view class="ORB" v-else>
						{{item.tihuo_name}}
					</view>
				</view>
			</view>
			<u-popup :show="show" mode="center" round="20">
				<view class="PBox">
					<view class="PTitle">
						确认提货？
					</view>
					<view class="PContont">
						<checkbox @click="changeyd" :checked="ty" /><text style="font-size: 28rpx;color: red;"
							@click="ToXY(1,4)">《平台免责协议》</text>
					</view>
					<view class="PBtn">
						<view class="LBtn" @click="close">
							取消
						</view>
						<view class="RBtn" @click="tihuo(zc)">
							确定
						</view>
					</view>
				</view>
			</u-popup>
			<view v-show="noMoreData" class="no-more-data">没有更多数据啦</view>
			<view v-show="!noMoreData && loading" class="loading">正在加载中...</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				page: 1,
				limit: 10,
				status: 0,
				noMoreData: false, // 是否已经加载完所有数据
				loading: false, // 是否正在加载中
				mainHeight: 0, // scroll-view 的高度，用于计算下拉刷新动画位置
				userInfo: {},
				show: false,
				zc: '',
				ty: true
			};
		},
		async onLoad(o) {
			this.status = await o.status - 0
			if (o.status - 0 == 1) {
				uni.setNavigationBarTitle({
					title: '摘单'
				})
			} else {
				uni.setNavigationBarTitle({
					title: '挂单'
				})
			}
			await this.user()
			await this.myorderlist()
		},
		methods: {
			close() {
				this.show = false
			},
			changeyd() {
				this.ty = !this.ty
			},
			user() {
				this.$api.userInfo({}, res => {
					this.userInfo = res.data
				})
			},
			myorderlist() {
				if (this.loading) return;
				this.loading = true;
				this.$api.myorderlist({
					page: this.page,
					limit: this.limit,
					status: this.status
				}, res => {
					const {
						data
					} = res;
					if (data.length < this.limit) {
						this.noMoreData = true;
					}
					this.list.push(...data);
					this.loading = false;
					this.page++;
				});
			},
			fahuo(e) {
				let that = this
				uni.showModal({
					content: '您确认要发货吗？',
					success(res) {
						if (res.confirm) {
							setTimeout(() => {
								uni.showModal({
									title: '您确认要发货吗？',
									success(res) {
										if (res.confirm) {
											that.$api.fahuo({
												order_id: e
											}, res => {
												if (res.status) {
													uni.showToast({
														title: '发货成功',
														success() {
															setTimeout(() => {
																that.list = []
																that.page =
																	1
																that.noMoreData =
																	false
																that.loading =
																	false
																that.myorderlist()
															}, 1500)
														}
													})
													// 提货成功后刷新订单列表

												} else {
													uni.showToast({
														title: res.msg,
														icon: 'none'
													})
												}
											})
										}
									}
								})
							}, 500)
						}
					}
				})
			},
			tihuo2(e) {
				this.show = !this.show
				this.zc = e
			},
			ToXY(A, B) {
				uni.navigateTo({
					url: `/pages/article/index?id_type=${A}&id=${B}`
				})
			},
			tihuo(e) {
				if (this.ty) {
					this.show = false
					let that = this
					uni.showModal({
						content: '确认提货？',
						success(res) {
							if (res.confirm) {
								that.$api.tihuo({
									order_id: e
								}, res => {
									if (res.status) {
										uni.showToast({
											title: '提货成功',
											success() {
												setTimeout(() => {
													that.list = []
													that.page = 1
													that.noMoreData = false
													that.loading = false
													that.myorderlist()
												}, 1500)
											}
										})
										// 提货成功后刷新订单列表

									} else {
										uni.showToast({
											title: res.msg,
											icon: 'none'
										})
									}
								})
							} else {

							}
						}
					})
				} else {
					uni.showToast({
						title: '请先阅读并同意免责协议',
						icon: 'none'
					})
				}
			},
			loadMoreData() {
				if (!this.noMoreData) {
					this.myorderlist();
				}
			},
		},
		mounted() {
			uni.getSystemInfo({
				success: (res) => {
					// #ifndef H5
					this.mainHeight = res.windowHeight - 125;
					// #endif
					// #ifdef H5
					this.mainHeight = res.windowHeight;
					// #endif
				},
			});
		},
	}
</script>

<style lang="scss" scoped>
	.PBox {
		width: 70vw;
		text-align: center;

		.PTitle {
			margin: 30rpx 0;
		}

		.PContont {
			margin: 50rpx 0 20rpx 0;
		}

		.PBtn {
			display: flex;
			color: #c992ff;
			border-top: 1rpx solid #b9b9b9;
			padding: 15rpx 0;

			.LBtn {
				flex: 1;
				text-align: center;
			}

			.RBtn {
				flex: 1;
				text-align: center;
			}
		}


	}

	.TopBox {
		border-radius: 20rpx;
		background: linear-gradient(180deg, #F2D7AA 0%, #DEBC86 100%);
		display: flex;
		align-items: center;
		padding: 30rpx 0 18rpx;
		margin: 20rpx 30rpx;

		.Line {
			width: 1rpx;
			height: 50rpx;
			background: #202126;
		}

		.Item {
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;

			.ItemTop {
				margin-bottom: 13rpx;
				font-size: 28rpx;
				color: #202126;
			}

			.ItemBottom {
				font-weight: bold;
				color: #202126;
				font-size: 36rpx;
			}
		}
	}

	.orderList {
		.orderItem {
			background-color: #202126;
			display: flex;
			align-items: center;
			padding: 15rpx;
			margin: 0 30rpx 20rpx 30rpx;
			border-radius: 20rpx;

			.OL {
				margin-right: 20rpx;

				image {
					width: 252rpx;
					height: 252rpx;
					border-radius: 20rpx;
				}
			}

			.OR {
				.ORT {
					font-weight: bold;
					color: #FFFFFF;
				}

				.ORM {
					margin: 11rpx 0 20rpx;
					color: #F2D7AA;
					font-size: 24rpx;

					text {
						font-size: 32rpx;
						font-weight: bold;
					}
				}

				.ORB {
					display: inline-block;
					padding: 10rpx 55rpx;
					font-size: 24rpx;
					color: #181818;
					background: #e7c897;
					border-radius: 10rpx;
				}

				.ORBB {
					display: inline-block;
					padding: 5rpx 30rpx;
					font-size: 24rpx;
					color: #b9b9b9;
					background: #181818;
					border-radius: 10rpx;
				}
			}
		}
	}

	.no-more-data,
	.loading {
		text-align: center;
		font-size: 24rpx;
		color: #ccc;
		padding: 20rpx 0;
	}

	.loading {
		color: #000;
	}
</style>